<?php
	 $KEY = "TSU0gS6otoJz6kXGiXcdXIeQ";
    $MCHNO = "CH02";
 	$TAG = "470";
	$VERSION = "1.0";
	$APPID = "1084";
	$API = "http://pay.playgame178.com/wfNewThreepayApi";
	$codeSequence = array('1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
	$count = 20;
	$length = count($codeSequence) - 1;
	

	if(isset($_GET["action"]) && $_GET["action"] =="generateOrderId"){
		$orderId = generateOrderId();
		$json['orderId'] = $orderId;
		echo json_encode($json);
		exit;
	}
	if(isset($_POST["action"]) && $_POST["action"] =="submitPay"){
		$json = submitPay();
		header("Content-Type:text/html;charset=utf-8");
		echo $json;
	}
		
	function generateOrderId(){
		$retOrderIdArr = array();
		for($i = 0; $i < $GLOBALS['count']; $i++){
			$retOrderIdArr[] = $GLOBALS['codeSequence'][rand(0, $GLOBALS['length'])];
		}
		return implode("", $retOrderIdArr);
	}
	
	function submitPay(){
		$json = warpParam();	
		$sign = sign($json);
		$json['sign']=$sign;
		$order = json_encode($json);
		//调用第三方下单
		$result = postCurl($order, array("content-type: application/json;charset=UTF-8"), $_SERVER['HTTP_USER_AGENT']);
		//$result=json_decode($result);
		return $result; 
	}
	
	//生成签名
	function sign($order){
		ksort($order);
		$buff = "";
		foreach ($order as $k => $v)
		{
			if($k != "sign" && $v != "" && !is_array($v)){
				$buff .= $k . "=" . $v . "&";
			}
		}
		$buff.='key='.$GLOBALS['KEY'];
		$string = strtolower(md5($buff));
		return $string;
	}
	
	function warpParam(){
		$subject = isset($_POST["subject"]) ? $_POST["subject"] : '';
		$totalFee = isset($_POST["total_fee"]) ? $_POST["total_fee"] : '';
		$body = isset($_POST["body"]) ? $_POST["body"] : '';
		$payType = isset($_POST["pay_type"]) ? $_POST["pay_type"] : '';
		$mchorderid = isset($_POST["mchorderid"]) ? $_POST["mchorderid"] : '';
		$showUrl = isset($_POST["show_url"]) ? $_POST["show_url"] : '';
		$mchAppId = isset($_POST["mch_app_id"]) ? $_POST["mch_app_id"] : '';
		$deviceInfo = isset($_POST["device_info"]) ? $_POST["device_info"] : '';
		$ua = isset($_POST["ua"]) ? $_POST["ua"] : '';
		$mchAppName = isset($_POST["mch_app_name"]) ? $_POST["mch_app_name"] : '';
		$callbackUrl = isset($_POST["callback_url"]) ? $_POST["callback_url"] : '';
		
		$order = array();
		if (isset($subject)) {
			$order["subject"] = trim($subject);
		}
		if (isset($totalFee)) {
			$order["total_fee"] = trim($totalFee);
		}
		if (isset($body)) {
			$order["body"] = trim($body);
		}
		if (isset($payType)) {
			$order["pay_type"] = trim($payType);
		}
		$order["mchNo"] = $GLOBALS['MCHNO'];
		$order["tag"] = $GLOBALS['TAG'];
		$order["version"] = $GLOBALS['VERSION'];
		$order["appid"] = $GLOBALS['APPID'];
		if (isset($mchorderid)) {
			$order["mchorderid"] = trim($mchorderid);
		}
		if (isset($showUrl)) {
			$order["show_url"] = trim($showUrl);
		}
		if (isset($mchAppId)) {
			$order["mch_app_id"] = trim($mchAppId);
		}
		if (isset($deviceInfo)) {
			$order["device_info"] = trim($deviceInfo);
		}
		if (isset($ua)) {
			$order["ua"] = trim($ua);
		}
		if (isset($mchAppName)) {
			$order["mch_app_name"] = trim($mchAppName);
		}
		if (isset($callbackUrl)) {
			$order["callback_url"] = trim($callbackUrl);
		}
		return $order;
	} 
	
	function postCurl($postfield, $hearderinfo=array(), $ua="MyLinuxCURL"){
		if(!$postfield){
			return false;
		}
		$ch  = curl_init();
		curl_setopt($ch, CURLOPT_URL, $GLOBALS['API']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30000);
		curl_setopt($ch, CURLOPT_TIMEOUT,30000);
		curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		if($hearderinfo){
			curl_setopt($ch,CURLOPT_HTTPHEADER,$hearderinfo);
		}
	
		$curl_return = curl_exec($ch);
		if(curl_errno($ch)){
			return false;    //curl出错
		}
		curl_close($ch);
		unset($ch);
		return $curl_return;
	}
?>
